<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Support\Facades\Notification;

class Announcement extends Model
{
    use HasFactory;
    protected $appends=['unread'];
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y/m/d H:i:s');
    }

    protected $guarded = [];
    protected function unread(): Attribute
    {
        return new Attribute(
            get: fn ($value) => $this->getUnread($value),
        );
    }
    protected function getUnread($value){
        //Notification::class
        return DatabaseNotification::query()
            ->where('data',$this->attributes['id'])
            ->whereNull('read_at')
            ->count();
    }

}
